<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LookupDialog.aspx.cs" Inherits="LookupDialog" %>
<%@ Register Src="~/Controls/GridControl.ascx" TagName="GridControl" TagPrefix="edn" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title id="headTitle" runat="server" meta:resourcekey="headTitle"></title>
    <base target="_self" />
    <meta http-equiv="Pragma" content="no-cache" />
</head>

<script type="text/javascript">

window.attachEvent('onbeforeunload', onclose); 

function onclose()
{
	if(IsNull(window.returnValue))
	{
		var returnValue = new Object();
		returnValue.exitcode = 1;
		returnValue.data = null;
		window.returnValue = returnValue;
	}
}

function onok()
{
	var oItems = preparelookupitems();
	if(oItems.length == 0)
	{
		alert("Please select at least one record from the list.");
		return;
	}
	var returnValue = new Object();
	returnValue.exitcode = 0;
	returnValue.data = oItems;
	returnValue.urlFormat = '<%=this.SelectedObject.OpenUrl %>';
	window.returnValue = returnValue;
	window.close();
}

function oncancel()
{
	window.close();
}

function preparelookupitems()
{
	var ddlObjectTypes = document.getElementById("<%= ddlObjectTypes.ClientID %>");
	var typename = ddlObjectTypes.options[ddlObjectTypes.selectedIndex].value;
	var oGridItems = document.crmGrid.getSelectedItems();
	var oItems = new Array();
	for(var I = 0; I < oGridItems.length; I++)
	{
		var oGridItem = oGridItems[I];
		var oLookup = new Object();
		oLookup.id = oGridItem.itemid;
		oLookup.name = oGridItem.itemlabel;
		oLookup.type = oGridItem.itemcode;
		oLookup.typename = typename;
		oItems[I] = oLookup;
	}
	return oItems;
}

function onremove()
{
	var oItems = preparelookupitems();
	if(oItems.length == 0)
	{
		alert("Please select at least one record from the list.");
		return;
	}
	var returnValue = new Object();
	returnValue.exitcode = 2;
	returnValue.data = oItems;
	returnValue.urlFormat = '<%=this.SelectedObject.OpenUrl %>';
	window.returnValue = returnValue;	
	window.close();
}

function onopen()
{
	var oGridItems = document.crmGrid.getSelectedItems();
	if(oGridItems.length == 0)
	{
		alert("Please select a record to open.");
		return;
	}
	var oGridItems = document.crmGrid.getSelectedItems();
	for(var i = 0; i < oGridItems.length; i++)
	{
		var url = formatString('<%=this.SelectedObject.OpenUrl %>', oGridItems[i].itemid);
		OpenWindow(url);
		//win.attachEvent("onunload", handleUnload);
	}
}

function onnew()
{
	var url = '<%=this.SelectedObject.NewUrl %>';
	var win = OpenWindow(url);
	win.attachEvent("onunload", handleUnload);
}

function handleUnload()
{
	setTimeout('refresh()', 500);
}

function refresh()
{
	document.crmGrid.refresh();
}

function onrowdblclick()
{
	setTimeout('onok()', 200);
}

</script>

<body>
	<asp:XmlDataSource ID="xdsLookupDialog" runat="server" DataFile="~/App_Data/LookupDialog.xml"></asp:XmlDataSource>
    <form id="form1" runat="server">
        <table style="width: 100%; height: 100%;" cellspacing="0" cellpadding="0">
            <tr>
                <td colspan="2" class="mscrm-Dialog-Header">
                    <div class="mscrm-Dialog-Header-Title" id="DlgHdTitle">
                        <asp:Literal ID="litHeaderTitle" runat="server" meta:resourcekey="litHeaderTitle" />
                    </div>
                    <div class="mscrm-Dialog-Header-Desc" id="DlgHdDesc">
                        <asp:Literal ID="litHeaderDesc" runat="server" meta:resourcekey="litHeaderDesc" />
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="height: 100%;">
                    <div class="mscrm-Dialog-Main">
                        <div id="divWarning" style="height: 100%;">
                            <table cellspacing="0" cellpadding="0" width="100%" height="100%">
                                <tr>
                                    <td class="mscrm-Dialog-Lookup-SearchArea">
                                        <table width="100%" cellpadding="0" cellspacing="0">
                                            <col/>
                                            <col width="70%"/>
                                            <col width="30%" />
                                            <tr>
                                                <td nowrap>
                                                    <span style="color: #000000; font-weight: bold">
														<asp:Literal runat="server" ID="litLookupTypes" meta:resourcekey="litLookupTypes"/>
													</span>
                                                </td>
                                                <td class="mscrm-Dialog-Lookup-Types">
                                                    <asp:DropDownList ID="ddlObjectTypes" runat="server" CssClass="mscrm-SelectBox" AutoPostBack="true" OnSelectedIndexChanged="ddlObjectTypes_SelectedIndexChanged">
													</asp:DropDownList>
                                                </td>
                                                <td class="mscrm-Dialog-Lookup-Types">
                                                    <asp:TextBox ID="txtSearch" runat="server" Width="100%" style="height:20px">
													</asp:TextBox>
                                                </td>
                                            </tr>
                                        </table>
                                        <table width="100%" cellpadding="2" cellspacing="0">
                                            <tr>
                                                <td style="height: 10px; font-size: 1px; border-bottom: 1px solid #CCCCCC;">
                                                    &nbsp;</td>
                                            </tr>
                                            <tr>
                                                <td style="height: 1px; font-size: 1px; border-top: 1px solid #FFFFFF;">
                                                    &nbsp;</td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <table height="100%" width="100%" id="tblFind" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td colspan="3" style="padding-top: 5px">
                                                    <edn:GridControl ID="crmGrid" runat="server" EnableViewState="false" AllowClientSideSelection="true" AutoGenerateColumns="false" DisplayStatusBar="true" OnQueryDataSource="crmGrid_QueryDataSource" OnItemDataBound="crmGrid_ItemDataBound" OnClientRowDblClick="onrowdblclick">
													</edn:GridControl>
                                                </td>
                                            </tr>
                                            <tr style="padding-top: 10px;" height="40">
                                                <td nowrap colspan="3">
													<button id="btnProperties" runat="server" class="mscrm-Button" meta:resourcekey="btnProperties" onclick="onopen()"/>
													<span style="width: 5px;"></span>
													<button id="btnNew" runat="server" class="mscrm-Button" onclick="onnew()" meta:resourcekey="btnNew"/>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="mscrm-Dialog-Footer mscrm-Dialog-Footer-Left">
                    &nbsp;
                </td>
                <td class="mscrm-Dialog-Footer mscrm-Dialog-Footer-Right">
                    <button id="btnOK" runat="server" onclick="onok();" class="mscrm-Button" meta:resourcekey="btnOK" />
                    &nbsp;
                    <button id="btnCancel" runat="server" onclick="oncancel();" class="mscrm-Button" meta:resourcekey="btnCancel" />
                    &nbsp;
                    <button id="btnRemoveValue" runat="server" onclick="onremove();" class="mscrm-Button" meta:resourcekey="btnRemoveValue"/>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
